package edu.zut.cs.software.perfect.process.service;

import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.logging.log4j.LogManager;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import edu.zut.cs.software.perfect.base.service.GenericGenerator;
import edu.zut.cs.software.perfect.process.domain.ProcessRegistrar;

/**
 * @ClassName ProcessRegistrarEntityGenerator
 * @Description TODO
 * @Author niu-xi
 * @Date 2020年5月30日 上午12:09:34
 * @Version 1.0
 */
public class ProcessRegistrarEntityGenerator extends GenericGenerator{
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LogManager.getLogger(ProcessRegistrarEntityGenerator.class.getName());
	
	List<ProcessRegistrar> processRegistrarList;
	
	CourseProcessManager courseProcessManager;
	
	ProcessRegistrarManager processRegistrarManager;

	@Autowired
	public void setCourseProcessManager(CourseProcessManager courseProcessManager) {
		this.courseProcessManager = courseProcessManager;
	}

	@Autowired
	public void setProcessRegistrarManager(ProcessRegistrarManager processRegistrarManager) {
		this.processRegistrarManager = processRegistrarManager;
	}
	
	@Before
	public void setUp() throws Exception {
		this.processRegistrarList = new ArrayList<ProcessRegistrar>();
		InputStream input =ProcessRegistrarManagerTest.class.getResourceAsStream("/ProcessRegistrar_software.xlsx");
		@SuppressWarnings("resource")
		XSSFWorkbook wb = new XSSFWorkbook(input);
		XSSFSheet sheet = wb.getSheetAt(0);
		DataFormatter dataFormatter = new DataFormatter();
		for (int i = 1; i <= sheet.getLastRowNum(); i++) {
			Row row = sheet.getRow(i);
			ProcessRegistrar s = new ProcessRegistrar();
			for (int j = 0; j < row.getLastCellNum(); j++) {
				Cell cell = row.getCell(j);
				if (cell != null) {
					String value = dataFormatter.formatCellValue(cell);
					if (j == 0)
						s.setCourseNumber(value);
					if (j == 1)
						s.setCourseName(value);			
				}
			}
			this.processRegistrarList.add(s);
		}
	}
	@Test
	public void test() {
		this.processRegistrarManager.deleteAll();
		this.processRegistrarManager.save(this.processRegistrarList);
		Iterable<ProcessRegistrar> result = this.processRegistrarManager.findAll();
		if (logger.isInfoEnabled()) {
			logger.info("test() - Iterable<ProcessRegistrar> result={}", result); //$NON-NLS-1$
		}
		
		
	}

	/*
	CourseProcessManager courseProcessManager;
	
	ProcessRegistrarManager processRegistrarManager;

	@Autowired
	public void setCourseProcessManager(CourseProcessManager courseProcessManager) {
		this.courseProcessManager = courseProcessManager;
	}

	@Autowired
	public void setProcessRegistrarManager(ProcessRegistrarManager processRegistrarManager) {
		this.processRegistrarManager = processRegistrarManager;
	}
	
	@Test
	public void testBuild() {
		int num_processRegistrar = 3;
		int num_courseProcess = 3;
		for (int i = 0; i < num_processRegistrar; i++) {
			ProcessRegistrar processRegistrar = new ProcessRegistrar();
			processRegistrar.setCourseNumber("courseNumber_"+i);
			processRegistrar.setCourseName("courseName_" + i);			
			processRegistrar = this.processRegistrarManager.save(processRegistrar);
			
			for (int j = 0; j < num_processRegistrar; j++) {
				ProcessRegistrar c = new ProcessRegistrar(); 
				c.setCourseName("courseName_" + i + "_" + j);
				processRegistrar.setCourseNumber("courseNumber_"+i+"_"+j);
				c.setParent(processRegistrar); 
				c = this.processRegistrarManager.save(c);
				for (int k = 0; k < num_courseProcess; k++) {
					CourseProcess courseProcess = new CourseProcess();
					courseProcess.setClassId("classId_" + i + "_" + j + "_" + k);
					courseProcess.setCourseNumber("courseNumber_"+ i);
					courseProcess.setCourseName("courseName_"+ i);
					courseProcess.setProcessRegistrar(c); 
					courseProcess.setClassPlace(100+k+"_classroom");
					courseProcess = this.courseProcessManager.save(courseProcess);
					assertNotNull(courseProcess);
				}
			}
		}
	}
	*/
	
}
